package com.irdstudio.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.service.facade.GrantAuthInfoService;
import com.irdstudio.efp.console.service.vo.GrantAuthDetailVO;
import com.irdstudio.efp.console.service.vo.GrantAuthInfoVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class GrantAuthInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("grantAuthInfoService")
	private GrantAuthInfoService grantAuthInfoService;
	
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/grant/auth/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<GrantAuthInfoVO>> queryGrantAuthInfoAll(
			@RequestBody GrantAuthInfoVO vo) {		
		// 根据用户角色获取用户权限方案
		List<GrantAuthInfoVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = grantAuthInfoService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = grantAuthInfoService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = grantAuthInfoService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = grantAuthInfoService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);

	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/grant/auth/info/{grantNo}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<GrantAuthInfoVO> queryByPk(@PathVariable("grantNo") String grantNo) {		
		GrantAuthInfoVO inVo = new GrantAuthInfoVO();
		inVo.setGrantNo(grantNo);
		GrantAuthInfoVO outputVo = grantAuthInfoService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param grantAuthInfo
	 * @return
	 */
	@RequestMapping(value="/grant/auth/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody GrantAuthInfoVO inGrantAuthInfoVo) {		
		int outputVo = grantAuthInfoService.deleteByPk(inGrantAuthInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inGrantAuthInfoVo
	 * @return
	 */
	@RequestMapping(value="/grant/auth/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody GrantAuthInfoVO inGrantAuthInfoVo) {		
		setUserInfoToVO(inGrantAuthInfoVo);
		int outputVo = grantAuthInfoService.updateByPk(inGrantAuthInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 状态生效更新信息
	 * @param inGrantAuthInfoVo
	 * @return
	 */
	@RequestMapping(value="/grant/auth/commit/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByCommit(@RequestBody GrantAuthInfoVO inGrantAuthInfoVo) {		
		setUserInfoToVO(inGrantAuthInfoVo);
		int outputVo = grantAuthInfoService.updateByCommit(inGrantAuthInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 状态失效更新信息
	 * @param inGrantAuthInfoVo
	 * @return
	 */
	@RequestMapping(value="/grant/auth/status/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByStatus(@RequestBody GrantAuthInfoVO inGrantAuthInfoVo) {		
		setUserInfoToVO(inGrantAuthInfoVo);
		int outputVo = grantAuthInfoService.updateByStatus(inGrantAuthInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inGrantAuthInfoVo
	 * @return
	 */
	@RequestMapping(value="/grant/auth/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<GrantAuthInfoVO> insertGrantAuthInfo(@RequestBody GrantAuthInfoVO inGrantAuthInfoVo) {
		setUserInfoToVO(inGrantAuthInfoVo);
		GrantAuthInfoVO outputVo = grantAuthInfoService.insertGrantAuth(inGrantAuthInfoVo);
		return getResponseData(outputVo);
		
	}
	
	
	//授权明细controller
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/grant/auth/duty/details", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<GrantAuthDetailVO>> queryGrantAuthDetailAll(
			@RequestBody GrantAuthDetailVO vo) {
		vo.setGrantType("01");
		// 根据用户角色获取用户权限方案
		List<GrantAuthDetailVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = grantAuthInfoService.queryAllDetailOwner(vo);
			break;
		case CurrOrg:
			outputVo = grantAuthInfoService.queryAllDetailCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = grantAuthInfoService.queryAllDetailCurrDownOrg(vo);
			break;
		default:
			outputVo = grantAuthInfoService.queryAllDetailOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/grant/auth/user/details", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<GrantAuthDetailVO>> queryGrantAuthDetail(
			@RequestBody GrantAuthDetailVO vo) {
		vo.setGrantType("02");
		// 根据用户角色获取用户权限方案
		List<GrantAuthDetailVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = grantAuthInfoService.queryAllDetailOwner(vo);
			break;
		case CurrOrg:
			outputVo = grantAuthInfoService.queryAllDetailCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = grantAuthInfoService.queryAllDetailCurrDownOrg(vo);
			break;
		default:
			outputVo = grantAuthInfoService.queryAllDetailOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/grant/auth/detail/{detailId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<GrantAuthDetailVO> queryByDetail(@PathVariable("detailId") String detailId) {		
		GrantAuthDetailVO inVo = new GrantAuthDetailVO();
		inVo.setDetailId(detailId);
		GrantAuthDetailVO outputVo = grantAuthInfoService.queryByDetailId(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据授权编号查询详情
	 * @return
	 */
	@RequestMapping(value="/grant/auth/grantno/details", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<GrantAuthDetailVO>> queryByGrantNo(
			@RequestBody GrantAuthDetailVO inVo) {		
		inVo.setGrantType("01");
		List<GrantAuthDetailVO> outputVo = grantAuthInfoService.queryByGrantNo(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据授权编号查询详情
	 * @return
	 */
	@RequestMapping(value="/grant/auth/grantno/s/details", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<GrantAuthDetailVO>> queryByDetailsGrantNo(
			@RequestBody GrantAuthDetailVO inVo) {		
		inVo.setGrantType("02");
		List<GrantAuthDetailVO> outputVo = grantAuthInfoService.queryByGrantNo(inVo);
		return getResponseData(outputVo);
		
	}
	
	
	
	/**
	 * 根据主键删除信息
	 * @param grantAuthDetail
	 * @return
	 */
	@RequestMapping(value="/grant/auth/detail", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody GrantAuthDetailVO inGrantAuthDetailVo) {		
		int outputVo = grantAuthInfoService.deleteByDetailId(inGrantAuthDetailVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inGrantAuthDetailVo
	 * @return
	 */
	@RequestMapping(value="/grant/auth/detail", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody GrantAuthDetailVO inGrantAuthDetailVo) {		
		setUserInfoToVO(inGrantAuthDetailVo);
		int outputVo = grantAuthInfoService.updateByDetailId(inGrantAuthDetailVo);
		ResponseData<Integer> responseData = new ResponseData<>();
		responseData.setCode(outputVo+"");
		responseData.setRows(outputVo);
		responseData.setSuccess(false);
		if(outputVo == -2){
			responseData.setMessage("最小授信金额与最大金额不能有交叉");			
			return responseData;
			
		}else if(outputVo == -3){
			responseData.setMessage("相同的岗位代码或者角色代码配置已经存在，不能修改！");			
			return responseData;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inGrantAuthDetailVo
	 * @return
	 */
	@RequestMapping(value="/grant/auth/detail", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertGrantAuthDetail(@RequestBody GrantAuthDetailVO inGrantAuthDetailVo) {
		setUserInfoToVO(inGrantAuthDetailVo);
		int outputVo = grantAuthInfoService.insertGrantAuthDetail(inGrantAuthDetailVo);
		ResponseData<Integer> responseData = new ResponseData<>();
		responseData.setCode(outputVo+"");
		responseData.setRows(outputVo);
		responseData.setSuccess(false);
		
		if(outputVo == -2){
			responseData.setMessage("最小授信金额与最大金额不能有交叉");			
			return responseData;
			
		}else if(outputVo == -3){
			responseData.setMessage("相同的岗位代码或者角色代码配置已经存在，不能再新增！");			
			return responseData;
		}
		return getResponseData(outputVo);
		
	}
}
